package cn.codesugar.material.design

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.core.view.GravityCompat
import androidx.recyclerview.widget.GridLayoutManager
import cn.codesugar.material.design.databinding.ActivityMainBinding
import com.google.android.material.snackbar.Snackbar
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {

    private var b: ActivityMainBinding? = null
    private val bears = mutableListOf(
        Bear("blue bear", R.drawable.bear1), Bear("Bear head", R.drawable.bear_2),
        Bear("brown bear", R.drawable.bear3), Bear("line bear", R.drawable.bear4),
        Bear("small brown bear", R.drawable.bear5), Bear("black bear", R.drawable.bear6),
        Bear("Bear head", R.drawable.bear7), Bear("Bear market", R.drawable.bear8),
        Bear("black Bear head", R.drawable.bear9), Bear("Love Bear", R.drawable.bear10),
        Bear("Toy Bear", R.drawable.bear11), Bear("Quadruped Bear", R.drawable.bear12),
        Bear("Uninhibited Bear", R.drawable.bear13), Bear("Purple Bear", R.drawable.bear14),
        Bear("Blue Toy Bear", R.drawable.bear15), Bear("Blue Bear Head", R.drawable.bear16),
        Bear("Stroke Bear head", R.drawable.bear17), Bear("Blue Stroke Bear head", R.drawable.bear18), )
    private val bearList = ArrayList<Bear>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        b = ActivityMainBinding.inflate(layoutInflater)
        setContentView(b?.root)
        setSupportActionBar(b?.toolbar)
        supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
            it.setHomeAsUpIndicator(R.drawable.ic_menu_vector)
        }
        b?.navView?.setCheckedItem(R.id.navCall)
        b?.navView?.setNavigationItemSelectedListener {
            b?.drawerLayout?.closeDrawers()
            true
        }
        b?.fab?.setOnClickListener {
            b?.fab?.let { v ->
                Snackbar.make(v, "Data deleted", Snackbar.LENGTH_SHORT)
                    .setAction("confirm") {
                        Toast.makeText(this, "You clicked FAB", Toast.LENGTH_SHORT).show()
                    }.show()
            }
        }
        initBears()
        val gridLayoutManager = GridLayoutManager(this, 2)
        val bearAdapter = BearAdapter(this, bearList)
        b?.recyclerView?.apply {
            setHasFixedSize(true)
            layoutManager = gridLayoutManager
            adapter = bearAdapter
        }
        b?.swipeRefresh?.setColorSchemeResources(R.color.colorPrimary)
        b?.swipeRefresh?.setOnRefreshListener {
            refreshFruits(bearAdapter)
        }

    }

    private fun initBears() {
        bearList.clear()
        repeat(50) {
            val index = (0 until bears.size).random()
            bearList.add(bears[index])
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun refreshFruits(adapter: BearAdapter) {
        thread {
            Thread.sleep(2000)
            runOnUiThread {
                initBears()
                adapter.notifyDataSetChanged()
                b?.swipeRefresh?.isRefreshing = false
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.backup -> {
                Toast.makeText(this, "You clicked Backup", Toast.LENGTH_SHORT).show()
            }
            R.id.delete -> {
                Toast.makeText(this, "You clicked Delete", Toast.LENGTH_SHORT).show()
            }
            R.id.settings -> {
                Toast.makeText(this, "You clicked Settings", Toast.LENGTH_SHORT).show()
            }
            android.R.id.home -> {
                Toast.makeText(this, "You clicked home", Toast.LENGTH_SHORT).show()
                b?.drawerLayout?.openDrawer(GravityCompat.START)
            }
        }
        return true
    }
}